package com.example.demo;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private SQLiteDatabase db;
    private TextView txtData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtData = findViewById(R.id.txt_data);

        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createDatabase();
            }
        });

        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                createStudenTable();
            }
        });

        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                insertIntoTable();
            }
        });

        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                readDataFromDB();
            }
        });
    }

    private void readDataFromDB() {
        if (db == null){
            createDatabase();
        }

        Cursor cursor = db.query("student",null,null,null,null,null,null);

        if (cursor == null||cursor.getCount()==0){
            return;
        }

        StringBuffer buffer = new StringBuffer();
        for (cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
            int stuno = cursor.getInt(0);
            String name = cursor.getString(1);
            int age = cursor.getInt(2);
            Log.e("readDataFromDB", "stuno ="+stuno +"name ="+name +"age ="+age);
            buffer.append(stuno).append("  ").append(name).append("  ").append(age).append("\n");
        }

        txtData.setText(buffer);
    }

    private void insertIntoTable() {
        if (db == null) {
            createDatabase();
        }

        //第一种方法
//        String sql = "insert into student values (171001,'chenhuayou',20)";
//        db.execSQL(sql);

        //第二种方法
        ContentValues values =new ContentValues();
        values.put("stuno",171002);
        values.put("name","you");
        values.put("age",21);
        db.insert("student",null,values);
    }

    private void createStudenTable() {
        if (db==null){
            createDatabase();
        }

        String sql = "create table if not exists student(stuno int,name varchar(20),age int)";
        db.execSQL(sql);
    }

    private void createDatabase() {
        String path = getFilesDir().getAbsolutePath() + File.separator + "stu.db";
        Log.e("MainActivity","path = "+path);
        db = SQLiteDatabase.openOrCreateDatabase(path,null);
    }
}
